package cn.stylefeng.guns.modular.business.frequency.orderpickassigndriver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.orderpickassigndriver.entity.OrderPickAssignDriver;
import cn.stylefeng.guns.modular.business.frequency.orderpickassigndriver.mapper.OrderPickAssignDriverMapper;
import cn.stylefeng.guns.modular.business.frequency.orderpickassigndriver.model.param.OrderPickAssignDriverParam;
import cn.stylefeng.guns.modular.business.frequency.orderpickassigndriver.service.OrderPickAssignDriverService;
import cn.stylefeng.guns.modular.business.frequency.orderpickassigndriver.enums.OrderPickAssignDriverExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * tt_order_pick_assign_driver 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderPickAssignDriverServiceImpl extends ServiceImpl<OrderPickAssignDriverMapper, OrderPickAssignDriver> implements OrderPickAssignDriverService {

    @Override
    public PageResult<OrderPickAssignDriver> page(OrderPickAssignDriverParam orderPickAssignDriverParam) {

        // 构造条件
        LambdaQueryWrapper<OrderPickAssignDriver> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderPickAssignDriver> list(OrderPickAssignDriverParam orderPickAssignDriverParam) {

        // 构造条件
        LambdaQueryWrapper<OrderPickAssignDriver> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderPickAssignDriverParam orderPickAssignDriverParam) {

        // 将dto转为实体
        OrderPickAssignDriver orderPickAssignDriver = new OrderPickAssignDriver();
        BeanUtil.copyProperties(orderPickAssignDriverParam, orderPickAssignDriver);

        this.save(orderPickAssignDriver);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderPickAssignDriverParam orderPickAssignDriverParam) {
        this.removeById(orderPickAssignDriverParam.getId());
    }

    @Override
    public void edit(OrderPickAssignDriverParam orderPickAssignDriverParam) {

        // 根据id查询实体
        OrderPickAssignDriver orderPickAssignDriver = this.queryOrderPickAssignDriver(orderPickAssignDriverParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderPickAssignDriverParam, orderPickAssignDriver);

        this.updateById(orderPickAssignDriver);
    }

    @Override
    public OrderPickAssignDriver detail(OrderPickAssignDriverParam orderPickAssignDriverParam) {
        return this.queryOrderPickAssignDriver(orderPickAssignDriverParam);
    }



    /**
     * 获取tt_order_pick_assign_driver
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderPickAssignDriver queryOrderPickAssignDriver(OrderPickAssignDriverParam orderPickAssignDriverParam) {
        OrderPickAssignDriver orderPickAssignDriver = this.getById(orderPickAssignDriverParam.getId());
        if (ObjectUtil.isEmpty(orderPickAssignDriver)) {
            throw new ServiceException(OrderPickAssignDriverExceptionEnum.NOT_EXISTED);
        }
        return orderPickAssignDriver;
    }
}